<?php

namespace App\Models\Common;

use App\Models\Tools\Data;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Common extends Model
{
    /**
     * 发送微信模板消息
     * @param string $openid        接收人openid
     * @param string $blade_name   通知类型(微信推送模板id)
     * @param array  $data          通知内容
     * @param string $page          打开页面
     * @return bool 是否发送成功
     */
    public static function sendLaifeiMessage(string $openid, string $blade_name, array $data, string $page = null)
    {
        //判断现在是否是本地环境，如果是本地环境不发送推送通知
        if (env('APP_ENV') === 'local') return true;

        $app = app('wechat.official_account');

        try{
            $result = $app->template_message->send([
                "touser" => $openid,
                "template_id" => Data::$wechat_blade[env('APP_ENV')][$blade_name],
                "url" => $page? env('APP_LAIFEI_URL') . $page: '',
                "data" => $data,
            ]);
            if($result['errcode'] === 0){
                Log::channel('template')->info("(" . $openid . ")<" . $page . ">" . Data::$wechat_blade_name[$blade_name], ["params"=>$data]);
                return true;//发送成功
            }else{
                Log::channel('template')->notice("(" . $openid . ")<" . $page . ">" . Data::$wechat_blade_name[$blade_name], ["params"=>$data]);
                return false;//发送失败
            }
        }catch (\Exception $e){
            Log::channel('template')->error("(" . $openid . ")<" . $page . ">" . Data::$wechat_blade_name[$blade_name], ["params"=>$data]);
            Log::error(Common::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return false;
        }
    }

    /**
     * 发送微信模板消息(批量)
     * @param array $openids        接收人openid
     * @param string $blade_name   通知类型(微信推送模板id)
     * @param array  $data          通知内容
     * @param string $page          打开页面
     * @return bool 是否发送完成
     */
    public static function sendLaifeiMessages(array $openids, string $blade_name, array $data, string $page = null)
    {
        //判断现在是否是本地环境，如果是本地环境不发送推送通知
        if (env('APP_ENV') === 'local') return true;

        $app = app('wechat.official_account');

        foreach($openids as $openid){
            try{
                $result = $app->template_message->send([
                    "touser" => $openid,
                    "template_id" => Data::$wechat_blade[env('APP_ENV')][$blade_name],
                    "url" => $page? env('APP_LAIFEI_URL') . $page: '',
                    "data" => $data,
                ]);
                if($result['errcode'] === 0){
                    Log::channel('template')->info("(" . $openid . ")<" . $page . ">" . Data::$wechat_blade_name[$blade_name], ["params"=>$data]);
                }else{
                    Log::channel('template')->notice("(" . $openid . ")<" . $page . ">" . Data::$wechat_blade_name[$blade_name], ["params"=>$data]);
                }
            }catch (\Exception $e){
                Log::channel('template')->error("(" . $openid . ")<" . $page . ">" . Data::$wechat_blade_name[$blade_name], ["params"=>$data]);
                Log::error(Common::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
                return false;
            }
        }
        return true;
    }

    /**
     * 查询所有的等级渠道
     * @return array channels
     */
    public static function getChannels()
    {
        $r_channel = Redis::hgetall('explore_channels');
        if($r_channel){
            $channels = [];
            foreach($r_channel as $channel){
                $channels[] = json_decode($channel, true);
            }
        } else {
            $channels = DB::table('explore_channel')->select('channel_id','channel_name')->get()->toArray();
            $channels = json_decode(json_encode($channels), true);
            foreach($channels as $channel){
                Redis::hset('explore_channels',$channel['channel_id'],json_encode($channel));
            }
            Redis::expire('explore_channels',7200);
        }
        return $channels;
    }
}
